﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml;

namespace An_Im.Test
{
    /// <summary>
    /// 用于解析配置文件的数据
    /// </summary>
    internal class SystemConfig
    {
        /// <summary>
        /// 配置文件。
        /// </summary>
        private XmlDocument xmldoc = null;
        private XmlElement xmlroot => xmldoc["安AM"];

        /// <summary>
        /// 模板路径。
        /// </summary>
        public string TemplateFileFullPath => xmlroot["应用信息"].GetAttribute("模板路径");

        /// <summary>
        /// 模板路径。
        /// </summary>
        public string TemplateFileName => xmlroot["应用信息"].GetAttribute("模板名称");

        /// <summary>
        /// 功能集合
        /// </summary>
        public List<DetailFunctionCollection> DetailFunctionCollections;


        /// <summary>
        /// 构造函数
        /// </summary>
        public SystemConfig(string path)
        {
            //var path = GetConfigFullPath();
            if (File.Exists(path))
            {
                xmldoc = new XmlDocument();
                xmldoc.LoadXml(File.ReadAllText(path));

                DetailFunctionCollections = new List<DetailFunctionCollection>();
                foreach (var module in xmlroot["功能列表"].OfType<XmlElement>())
                {
                    var detailFunctionCollection = new DetailFunctionCollection();
                    detailFunctionCollection.Title = module.GetAttribute("名称");

                    foreach (var funcNode in module.OfType<XmlElement>())
                    {
                        var func = new DetailFunction();
                        func.Description = funcNode.GetAttribute("描述");
                        func.SlideIndex = int.Parse(funcNode.GetAttribute("幻灯片编号"));
                        func.ShapeIndex = funcNode.GetAttribute("图形名称");
                        func.Name = funcNode.GetAttribute("名称");

                        detailFunctionCollection.Add(func);
                    }

                    DetailFunctionCollections.Add(detailFunctionCollection);
                }
              
            }



        }

        public SystemConfig() : this(GetConfigFullPath())
        {

        }

        /// <summary>
        /// 尝试获得配置文件的完整路径。
        /// </summary>
        /// <returns></returns>
        private static string GetConfigFullPath()
        {
            return Path.Combine(Environment.CurrentDirectory, "AnimationConfig.xml");
        }

    }
}
